package subumbo.game.highscore
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	
	/**
	 * Data proxy for subumbo highscore server service.
	 * 
	 * Singleton: use .instance to access.
	 * 
	 * @author patrickwolleb
	 * 
	 */	
	public class HighscoreProxy extends URLLoader
	{
		private var KEY:String;
		private var VALUE:String;
	
		/**
		 * Initialises proxy with the key and the value to gain access to the server.  
		 * @param key
		 * @param value
		 * 
		 */		
		public function init(key:String, value:String):void
		{
			KEY = key;
			value = value;
		}
		
		
		/**
		 * Loads data asynchronously. Attach Event.COMPLETE listener and parse data property
		 * @param game The Game the score was stored for.
		 */		
		public function loadScores(game:String):void
		{
			load(new URLRequest("http://www.subumbo.com/highscore/get.php?game="+game+"&"+KEY+"="+VALUE));
			this.addEventListener(Event.COMPLETE, onScoresLoaded);
		}
		protected function onScoresLoaded(e:Event):void
		{
			
		}
		
		
		/**
		 * Submit data asynchronously. Attach Event.COMPLETE listener. Return value is "Inserted" on success and "Failed" on failed.
		 * @param game The Game the score was stored for.
		 * @param username The username the score is saved for
		 * @param score
		 */		
		public function submitScore(game:String, username:String, score:String):void
		{
			load(new URLRequest("http://www.subumbo.com/highscore/submit.php?game="+game+"&"+KEY+"="+VALUE+"&score="+score+"&username="+username));
			this.addEventListener(Event.COMPLETE, onScoresSubmited);
		}
		protected function onScoresSubmited(e:Event):void
		{
				
		}
		
		
		
		static private var _instance:HighscoreProxy;
		static public function get instance():HighscoreProxy
		{
			if(!_instance) _instance = new HighscoreProxy();
			return _instance;
		}
		public function HighscoreProxy()
		{
			super(null);
		}
		
		
	}
}